#!/bin/bash

# Enter the directory where the script is located (Important!)
cd "$(dirname "$0")"

# Define variables
CXX="g++"
CXXFLAGS="-std=c++17 -Wall -g"
INCLUDE_DIR="../include"
OUTPUT="soulknight"

# Source file list (Note: each line must end with \ for continuation)
SOURCES="main.cpp \
../sources/GameManager.cpp \
../sources/Dungeon.cpp \
../sources/Character.cpp \
../sources/Enemy.cpp \
../sources/Equipment.cpp \
../sources/Skill.cpp \
../sources/Talent.cpp \
../sources/Attributes.cpp \
../sources/Room.cpp"

# Clean up old executable file
if [ -f "$OUTPUT" ]; then
    rm "$OUTPUT"
    echo "Old executable file deleted: $OUTPUT"
fi

# Clear the compilation error log file
ERROR_LOG="compile_errors.log"
> "$ERROR_LOG"

# Compile
echo "Compiling..."
$CXX $CXXFLAGS -I$INCLUDE_DIR $SOURCES -o $OUTPUT 2> "$ERROR_LOG"

# Check compilation result
if [ $? -ne 0 ]; then
    echo "Compilation failed! Error log saved to $ERROR_LOG"
    cat "$ERROR_LOG"
    exit 1
fi

# Check if the executable was created
if [ ! -f "$OUTPUT" ]; then
    echo "Error: Executable file $OUTPUT was not created!"
    exit 1
fi

# Run
echo "Compilation successful! Running $OUTPUT..."
./$OUTPUT